package com.winning.sx.microframework.common.config;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.winning.sx.microframework.common.util.AjaxUtil;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;

/**
 * mvc异常处理器
 * @author jelly
 *
 */
@Component
public class MvcExceptionResolver  implements HandlerExceptionResolver{
    private   Logger logger = Logger.getLogger(MvcExceptionResolver.class);

    @Override
    public ModelAndView resolveException(HttpServletRequest request,
                                         HttpServletResponse response, Object handler, Exception ex) {

        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        try {
            String errorMsg="";
            boolean isAjax= "1".equals(request.getParameter("isAjax"));

            //ex 为业务层抛出的自定义异常
            if(ex instanceof ServiceCustomException){
                ServiceCustomException customEx=    (ServiceCustomException)ex;

                errorMsg ="customStatus:"+customEx.getCustomStatus() +",customMessage:"+customEx.getCustomMessage()
                        +"\r\n"+ ExceptionUtils.getStackTrace(customEx);
                logger.error(errorMsg);
            }else{
                //ex为非自定义异常，则
                errorMsg=ExceptionUtils.getStackTrace(ex);
                logger.error(errorMsg);

            }

            if(isAjax){

                response.setContentType("application/json;charset=UTF-8");
                response.getWriter().write(JSON.toJSONString(AjaxUtil.messageMap(500, errorMsg)));
                return new   ModelAndView();
            }else{
                //否则，  输出错误信息到自定义的500.jsp页面
                ModelAndView mv = new ModelAndView("/error/500");
                mv.addObject("errorMsg", errorMsg);
                return mv ;
            }
        } catch (IOException e) {
            logger.error(ExceptionUtils.getStackTrace(e));
        }
        return new   ModelAndView();

    }

}